import 'package:flame/camera.dart';
import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter_flame/bloc/FlameBlocGame.dart';
import 'package:flutter_flame/bloc/game_bloc/GameBloc.dart';
import 'package:flutter_flame/bloc/game_bloc/GameState.dart';
import 'package:flutter_flame/testFunction/testFunction.dart';
import 'package:flutter_flame/worlds/game/GameWorld.dart';

import '../config/GameConfig.dart';
import '../worlds/game/components/TextInitSunlight.dart';
import '../worlds/welcome_menu/WelcomeMenuWorld.dart';

// 路由
class RouterGame extends FlameBlocGame<GameBloc, GameStatesState> {
  RouterGame(GameBloc bloc) : super(bloc: bloc) {
    // 设置camera可以让游戏画面根据窗口尺寸自动缩放且完整展示
    camera = CameraComponent.withFixedResolution(
      width: GameConfig.gameScreenWidth,
      height: GameConfig.gameScreenHeight,
    );
  }

  late final RouterComponent router;

  @override
  Future<void> onLoad() async {
    await add(
      FlameMultiBlocProvider(
        providers: [
          FlameBlocProvider<GameBloc, GameStatesState>(create: () => GameBloc())
        ],
        children: [TextInitSunlight()],
      ),
    );
    router = RouterComponent(
      initialRoute: TestFunction.initRoute(),
      routes: {
        WelcomeMenuWorld.routeName: WorldRoute(WelcomeMenuWorld.new),
        GameWorld.routeName: WorldRoute(() => GameWorld(bloc)),
      },
    );
    add(router);
  }
}
